package com.wzt.lianfirecontrol.fragment.my;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.BannerModel;
import com.wzt.lianfirecontrol.fragment.BaseFragment;
import com.wzt.lianfirecontrol.http.HttpHelper;
import com.wzt.lianfirecontrol.http.json.ParseJsonData;
import com.wzt.lianfirecontrol.http.Url;
import com.wzt.lianfirecontrol.utils.Utils;
import com.wzt.lianfirecontrol.view.InitView;

import java.util.HashMap;

/**
 * 联系客服
 */
public class ContactKeFuFragment extends BaseFragment {

    private BaseActivity activity;
    private Bundle bundle;
    private View itemContentView;

    private LinearLayout ll_contact_kefu;
    private TextView tv_kefu_tele;
    private ImageView iv_kefu_weixin;

    private View include_no_data;
    private View include_no_net;
    private View include_progress_bar;

    private static final int CONTACT_KEFU_REQUEST_WHAT = 0;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == itemContentView){
            bundle = getArguments();
            activity = getMyActivity();
            itemContentView = inflater.inflate(R.layout.f_contact_kefu, container, false);
            initView();
            // initHttpHelper();
        }
        if (itemContentView.getParent() != null) {
            ((ViewGroup) itemContentView.getParent()).removeView(itemContentView);
        }
        return itemContentView;
    }

    private void initView() {
        ll_contact_kefu = itemContentView.findViewById(R.id.ll_contact_kefu);
        tv_kefu_tele = itemContentView.findViewById(R.id.tv_kefu_tele);
        ll_contact_kefu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BannerModel bannerModel = new BannerModel();
                bannerModel.setActionType(ConstData.PHONE);
                bannerModel.setActionValue(tv_kefu_tele.getText().toString());
                activity.setClickAction(bannerModel);
            }
        });

        iv_kefu_weixin = itemContentView.findViewById(R.id.iv_kefu_weixin);

        include_no_data = itemContentView.findViewById(R.id.include_no_data);
        include_progress_bar = itemContentView.findViewById(R.id.include_progress_bar);
        InitView.instance().initProgressBar(activity, include_progress_bar);
        include_no_net = itemContentView.findViewById(R.id.include_no_net);
        include_no_net.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateData();
            }
        });
    }

    /**
     * 获取列表
     */
    private void initHttpHelper() {
        if (Utils.hasNetwork(activity)){
            include_no_net.setVisibility(View.GONE);
        }else {
            include_no_net.setVisibility(View.VISIBLE);
            return;
        }
        include_progress_bar.setVisibility(View.VISIBLE);
        HashMap<String, String> url_params = null;
        if (bundle != null && bundle.containsKey(ConstData.ACTIONPARAMS)){
            url_params = (HashMap<String, String>) bundle.getSerializable(ConstData.ACTIONPARAMS);
        }if (null == url_params){
            url_params = new HashMap<>();
        }
        String url = Url.getGetUrl(activity,  Url.HOME_URL, url_params);
        HttpHelper homeListHttpHelper = new ContactKefuListHttpHelper(activity, url, CONTACT_KEFU_REQUEST_WHAT, handler);
        homeListHttpHelper.setParseJsonData(new ContactKeFuParseJsonData());
        homeListHttpHelper.getHttpRequest();
    }


    private class ContactKefuListHttpHelper extends HttpHelper{
        public ContactKefuListHttpHelper(BaseActivity activity, String url, int what, Handler handler) {
            super(activity, url, what, handler);
        }
    }

    protected class ContactKeFuParseJsonData extends ParseJsonData {

        public ContactKeFuParseJsonData() {
        }

        @Override
        public void analyzeResult(String dataStr, Bundle bundle, int what) {
            switch (what){
                case CONTACT_KEFU_REQUEST_WHAT:
//                    try {
//                        if (objectData.has("list")){
//                            String rowsData = objectData.getString("list");
//                            LinkedList<BannerModel> bannerModels = (LinkedList<BannerModel>) JSONUtil.jsonArrayToBeanList(new JSONArray(rowsData), BannerModel.class);
//                            if (null != bannerModels && bannerModels.size() > 0){
//                                bundle.putSerializable("list", bannerModels);
//                            }
//                            bundle.putBoolean(REQUEST_OK, true);
//                        }
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                        bundle.putBoolean(REQUEST_OK, false);
//                        bundle.putString(ERROR_MSG, ANALYZE_ERROR_MSG);
//                    }
                    break;
            }
        }
    }

    @Override
    protected void dealWithMyMsgs(Message msg) {
        super.dealWithMyMsgs(msg);
        Bundle bundle = msg.getData();
        if (!bundle.getBoolean(ParseJsonData.REQUEST_OK) ||
                !ParseJsonData.REQUEST_OK_CODE.equals(bundle.getString(ParseJsonData.ERROR_CODE))){
            //ToastUtils.showToast(activity, bundle.getString(ParseJsonData.ERROR_MSG));
            if (msg.what == CONTACT_KEFU_REQUEST_WHAT) {
                include_progress_bar.setVisibility(View.GONE);
            }
            return;
        }
        switch (msg.what){
            case CONTACT_KEFU_REQUEST_WHAT:
                include_progress_bar.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void updateData() {
        super.updateData();
        initHttpHelper();
    }
}
